<?php

namespace app\models;

use yii;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;
use app\common\Functions;



class Cust extends ActiveRecord
{

    public $orderStaffID;      // 关联的订单员工号
    public $orderRemark;      // 订单描述
    public $parentProdName;
    public $attrcode;
    public $province;
    public $areas;
    public $city;
    public $prodInstAttrSpec;
    public $prodAttrInstCode;
    public $localAttrsInsts;
    public $custSourceTypeIDCnt;
    public $custTypeIDCnt;

    public $parentCustSourceID;
    //public $phone;//电话transfer


    public function rules()
    {
        return [

//            [['localAttrsInsts-3-content'], 'integer'],
//            ['localAttrsInsts[3][content]', 'required', 'message' => '请填写名称.'],

            ['parentCustSourceID', 'integer'],
            ['province', 'integer'],
            ['orderStaffID', 'integer'],
            ['orderRemark',  'string'],
            ['city', 'integer'],
            ['id', 'integer'],
            ['cust_type_id', 'integer'],
//            ['cust_name', 'required', 'message' => '请填写名称.'],
            ['cust_name', 'validateProdAttr'],
            ['region_id', 'integer'],
//            ['region_id', 'required', 'message' => '没有选择板块.'],
            ['cust_address', 'integer'],
            ['attrSpecs', 'validateProdAttr'],
            ['localAttrsInsts', 'validateProdAttr'],
            ['cust_source_id', 'integer', 'message' => '没有选择客户来源.'],
//            ['cust_source_id', 'required', 'message' => '没有选择客户来源.'],
            ['remark', 'string'],
            ['tel', 'validateProdAttr' ],
//            [['tel'], 'match', 'pattern' => '/^1(3|4|5|7|8)\d{9}$/', 'except' => 'social', 'message' => Yii::t('请填写正确的号码！')],//自定义验证


//            ['remark', 'required', 'message' => '请填写描述.'],


        ];

    }

    public function validatePhone($attribute, $params)
    {
//        // ['content']
//        if (!$this->localAttrsInsts[3][['content']])
//        {
//
//        }
//        foreach ($this->localAttrsInsts as $attrsInsts)
//        {
//            if ($attrsInsts)
//            {
//
//            }
//        }

//        return true;
    }


    public function getOrderRemark()
    {
        return $this->orderRemark;
    }
    public function getOrderStaffID()
    {
        return $this->orderStaffID;
    }

    public function validateProdAttr($attribute, $params)
    {
//        return true;
    }

    public function getAttrcode()
    {
        return $this->attrcode;
    }

    public function getProdAttrInstCode()
    {
        return $this->prodAttrInstCode;
    }


    public function getSubCustSources($parent_source_id=null)
    {
        $model = CustSource::find()->where(['parent_source_id' => $parent_source_id ])->orderBy('sort')->all();


        return ArrayHelper::map($model, 'id', 'cust_source_name');

    }


    public function getParentCustSourceID()
    {
        return $this->parentCustSourceID;
    }

    public function getProvince()
    {
        return $this->province;
    }

    public function getCity()
    {
        return $this->city;
    }

    public function getArea()
    {
        return $this->areas;
    }

    //最后一条记录的方法
    public function getOrderResumesText($id)
    {
        $sql  = 'SELECT `os`.remark AS s FROM   ';
        $sql .= Cust::tableName();

        $sql .= ' as `a`  LEFT JOIN' ;
        $sql .= Order::tableName();
        $sql .= ' as `o`  ON  `a`.`id` = `o`.`object_id` LEFT JOIN' ;
        $sql .= OrderResumes::tableName();
        $sql .= ' as `os`  ON  `o`.`id` = `os`.`order_id` LEFT JOIN' ;
        $sql .= ProdInst::tableName();
        $sql .= ' as `p`  ON  `p`.`id` = `os`.`peculiarity`  where a.id=:1  order by `os`.create_date desc' ;

        $useCustCount = Yii::$app->db->createCommand($sql)
            ->bindValue(':1', $id)
            ->queryOne();


        return $useCustCount['s'];
    }

    //算数量的方法
    public function getUseCustCount($id)
    {
        $sql  = 'SELECT COUNT(p.id) AS s FROM   ';
        $sql .= Cust::tableName();

        $sql .= ' as `a`  LEFT JOIN' ;
        $sql .= Order::tableName();
        $sql .= ' as `o`  ON  `a`.`id` = `o`.`object_id` LEFT JOIN' ;
        $sql .= OrderResumes::tableName();
        $sql .= ' as `os`  ON  `o`.`id` = `os`.`order_id` LEFT JOIN' ;
        $sql .= ProdInst::tableName();
        $sql .= ' as `p`  ON  `p`.`id` = `os`.`peculiarity`  where a.id=:1 ' ;

        $useCustCount = Yii::$app->db->createCommand($sql)
            ->bindValue(':1', $id)
            ->queryOne();

        return $useCustCount['s'];
    }



    public function getOrders()//订单
    {
        return $this->hasOne(Order::className(), ['object_id' => 'id'] );
    }

    public function getOrderResumess()//订单履历
    {
        return $this->hasOne(OrderResumes::className(), ['order_id' => 'id'])->viaTable(Order::tableName() . 'as o',  ['object_id' => 'id']);

    }

    public function getStaffs()
    {
        return $this->hasOne(Staff::className(), ['user_id' => 'staff_id'] );
    }

    public function getUsers()
    {
        return $this->hasOne(User::className(), ['id' => 'create_staff_id'] );
        // return $this->hasOne(Region::className(), ['id' => 'parent_region_id'])->viaTable(Region::tableName() . 'as d',  ['id' => 'region_id']);
    }

    public function getModifyUsers()
    {
        return $this->hasOne(User::className(), ['id' => 'modify_staff_id'] );
        // return $this->hasOne(Region::className(), ['id' => 'parent_region_id'])->viaTable(Region::tableName() . 'as d',  ['id' => 'region_id']);
    }

    public function getSuperParentRegions()
    {
        return $this->hasOne(Region::className(), ['id' => $this->parentRegions->parent_region_id]);
       // return $this->hasOne(Region::className(), ['id' => 'parent_region_id'])->viaTable(Region::tableName() . 'as d',  ['id' => 'region_id']);
    }

    public function getParentRegions()
    {
        return $this->hasOne(Region::className(), ['id' => 'parent_region_id'])->viaTable(Region::tableName() . 'as d',  ['id' => 'region_id']);
    }

    /* 获取楼盘信息*/
    public function getProdInstBuildings()
    {
        $where = [];
        $where['prod_spec_id'] = 10001;
        return $this->hasMany(ProdInst::className(), ['own_cust_id' => 'id'])->where($where);
        // return $this->hasOne(Region::className(), ['id' => 'parent_region_id'])->viaTable(Region::tableName() . 'as d',  ['id' => 'region_id']);
    }

    /* 获取房源信息 */
    public function getProdInstRooms()
    {
        $where = [];
        $where['prod_spec_id'] = 10002;
        return $this->hasMany(ProdInst::className(), ['own_cust_id' => 'id'])->where($where);
        // return $this->hasOne(Region::className(), ['id' => 'parent_region_id'])->viaTable(Region::tableName() . 'as d',  ['id' => 'region_id']);
    }

    /*取出当前产品的子集*/
    public function getSubProdInsts()
    {
        return $this->hasMany(ProdInst::className(), ['parent_id' => 'id']);
        // return $this->hasOne(Region::className(), ['id' => 'parent_region_id'])->viaTable(Region::tableName() . 'as d',  ['id' => 'region_id']);
    }

    /*取出当前产品的父集*/
    public function getParentProdInsts()
    {
        return $this->hasOne(ProdInst::className(), ['id' => 'parent_id']);
        // return $this->hasOne(Region::className(), ['id' => 'parent_region_id'])->viaTable(Region::tableName() . 'as d',  ['id' => 'region_id']);
    }

    public function getRegions()
    {
       // return $this->hasOne(Region::className(), ['region_id' => 'parent_region_id'])->viaTable(Region::className() . 'as c',  ['id' => 'region_id']);
        return $this->hasOne(Region::className(), ['id' => 'region_id']);
    }

    public function getCustExternalAttrs()
    {
        /**
         * 第一个参数为要关联的字表模型类名称，
         *第二个参数指定 通过子表的 customer_id 去关联主表的 id 字段
         */
        return $this->hasMany(CustExternalAttr::className(), ['cust_id' => 'id']);

    }

    /**
     * @name   getOneCustExternalAttrs
     * @description 取单条的CustExternalAttrs
     * @param
     * @return
     * @return mixed
     */
    public function getOneCustExternalAttrs()
    {
        /**
         * 第一个参数为要关联的字表模型类名称，
         *第二个参数指定 通过子表的 customer_id 去关联主表的 id 字段
         */
        return $this->hasOne(CustExternalAttr::className(), ['cust_id' => 'id']);

    }





    public function getCityList($parent_region_id=null)
    {
        $model = Region::find()->where(['parent_region_id' => $parent_region_id ])->orderBy('id')->all();


        return ArrayHelper::map($model, 'id', 'region_name');

    }

    public function getLocalAttrsInsts()
    {

        return $this->localAttrsInsts;
    }

    public function getAttrSpecs()
    {

        return $this->attrSpecs;
    }
}

?>
